<?php
namespace Library;

if( !defined('IN') ) die('Hacking Attempt');
/**
 * Description of Tools
 *
 * @author FFOZEU
 */

class Tools {

#################################################
# Affichage de la date de fin des enchères @int #
#################################################

    function afficherDate($time) {

        if( $time >= 86400 ) {

            $jour = floor($time/86400);
            $reste = $time%86400;

            $heure = floor($reste/3600);
            $reste = $reste%3600;

            $minute = floor($reste/60);

            $seconde = $reste%60;

            $sortie = $jour . 'j '.$heure . 'h ' . $minute . 'min ' . $seconde . 'sec';

        } elseif( $time < 86400 AND $time >= 3600 ) {

            $heure = floor($time/3600);
            $reste = $time%3600;

            $minute = floor($reste/60);

            $seconde = $reste%60;

            $sortie = $heure . 'h ' . $minute . 'min ' . $seconde . 'sec';

        } elseif( $time < 3600 AND $time >= 60 ) {

            $minute = floor($time/60);

            $seconde = $time%60;

            $sortie = $minute . 'min ' . $seconde . 's';

        } elseif( $time < 60 AND $time > 3 ) $sortie = $time . 's';
          elseif( $time == 3 ) $sortie = 'Une fois';
          elseif( $time == 2 ) $sortie = 'Deux fois';
          elseif( $time == 1 ) $sortie = 'Trois fois';

        return $sortie;

    }

######################
# Gestion du pluriel #
######################

    function pluriel($nbr) {

        if( $nbr > 1 ) return 's';

    }
    
#########################
#   Gestion des images  #
#########################
    /**
    * Check icon upload
    *
    * @param array $file Upload $_FILE value
    * @param integer $maxFileSize Maximum upload size
    */
    function checkIco($file, $maxFileSize)
    {
        if ($file['size'] > $maxFileSize)
            return ('Image is too large').' ('.($file['size'] / 1000).'ko). '.('Maximum allowed:').' '.($maxFileSize / 1000).'ko';
        if (substr($file['name'], -4) != '.ico')
            return ('Image format not recognized, allowed formats are: .ico');
        if ($file['error'])
            return ('Error while uploading image; please change your server\'s settings.');
        return false;
    }
    /**
    * Check image upload
    *
    * @param array $file Upload $_FILE value
    * @param integer $maxFileSize Maximum upload size
    */
    function checkImage($file, $maxFileSize)
    {
        if ($file['size'] > $maxFileSize)
            return 'Image is too large ('.($file['size'] / 1000).'KB). Maximum allowed: '.($maxFileSize / 1000).'KB';
        if (!isPicture($file))
            return 'Image format not recognized, allowed formats are: .gif, .jpg, .png';
        if ($file['error'])
            return 'Error while uploading image; please change your server\'s settings. Error code: '.$file['error'];
        return false;
    }
    /**
     *
     * @param type $file
     * @return type 
     */
    function checkImageUploadError($file)
    {
        if ($file['error'])
        {
            switch ($file['error'])
            {
                case 1:
                    return ('The file is too large.');
                    break;

            case 2:
                    return ('The file is too large.');
                    break;

                case 3:
                    return ('The file was partialy uploaded');
                    break;

                case 4:
                    return ('The file is empty');
                    break;
            }
        }
    }
    /**
     *
     * @param type $sourceFile
     * @param type $destFile
     * @param type $destWidth
     * @param type $destHeight
     * @param string $fileType
     * @return boolean 
     */
    function imageResize($sourceFile, $destFile, $destWidth = NULL, $destHeight = NULL, $fileType = 'jpg')
    {
        if (!file_exists($sourceFile))
            return false;
        list($sourceWidth, $sourceHeight, $type, $attr) = getimagesize($sourceFile);
        // If PS_IMAGE_QUALITY is activated, the generated image will be a PNG with .jpg as a file extension.
        // This allow for higher quality and for transparency. JPG source files will also benefit from a higher quality
        // because JPG reencoding by GD, even with max quality setting, degrades the image.
        if ($type == IMAGETYPE_PNG)
            $fileType = 'png';

        if (!$sourceWidth)
            return false;
        if ($destWidth == NULL) $destWidth = $sourceWidth;
        if ($destHeight == NULL) $destHeight = $sourceHeight;

        $sourceImage = $this->createSrcImage($type, $sourceFile);

        $widthDiff = $destWidth / $sourceWidth;
        $heightDiff = $destHeight / $sourceHeight;

        if ($widthDiff > 1 AND $heightDiff > 1)
        {
            $nextWidth = $sourceWidth;
            $nextHeight = $sourceHeight;
        }
        else
        {
            if ($widthDiff > $heightDiff)
            {
                $nextHeight = $destHeight;
                $nextWidth = round(($sourceWidth * $nextHeight) / $sourceHeight);
                $destWidth =  $nextWidth;
            }
            else
            {
                $nextWidth = $destWidth;
                $nextHeight = round($sourceHeight * $destWidth / $sourceWidth);
                $destHeight = $nextHeight;
            }
        }

        $destImage = imagecreatetruecolor($destWidth, $destHeight);

        // If image is a PNG and the output is PNG, fill with transparency. Else fill with white background.
        if ($fileType == 'png' && $type == IMAGETYPE_PNG)
        {
            imagealphablending($destImage, false);
            imagesavealpha($destImage, true);	
            $transparent = imagecolorallocatealpha($destImage, 255, 255, 255, 127);
            imagefilledrectangle($destImage, 0, 0, $destWidth, $destHeight, $transparent);
        }else
        {
            $white = imagecolorallocate($destImage, 255, 255, 255);
            imagefilledrectangle($destImage, 0, 0, $destWidth, $destHeight, $white);
        }

        imagecopyresampled($destImage, $sourceImage, (int)(($destWidth - $nextWidth) / 2), (int)(($destHeight - $nextHeight) / 2), 0, 0, $nextWidth, $nextHeight, $sourceWidth, $sourceHeight);

        return ($this->returnDestImage($fileType, $destImage, $destFile));
    }
    
    /**
     * generate image
     * @param type $type
     * @param type $ressource
     * @param type $filename
     * @return type 
     */
    function returnDestImage($type, $ressource, $filename)
    {
        $flag = false;
        switch ($type)
        {
            case 'gif':
                $flag = imagegif($ressource, $filename);
                break;
            case 'png':
                $quality = 7 ;
                $flag = imagepng($ressource, $filename, (int)$quality);
                break;		
            case 'jpg':
            case 'jpeg':
            default:
                $quality = 90 ;
                $flag = imagejpeg($ressource, $filename, (int)$quality);
                break;
        }
        imagedestroy($ressource);
        @chmod($filename, 0664);
        return $flag;
    }
    /**
     * image cut
     * @param type $srcFile
     * @param type $destFile
     * @param type $destWidth
     * @param type $destHeight
     * @param type $fileType
     * @param type $destX
     * @param type $destY
     * @return boolean 
     */
    function imageCut($srcFile, $destFile, $destWidth = NULL, $destHeight = NULL, $fileType = 'jpg', $destX = 0, $destY = 0)
    {
        if (!isset($srcFile['tmp_name']) OR !file_exists($srcFile['tmp_name']))
            return false;

        // Source infos
        $srcInfos = getimagesize($srcFile['tmp_name']);
        $src['width'] = $srcInfos[0];
        $src['height'] = $srcInfos[1];
        $src['ressource'] = createSrcImage($srcInfos[2], $srcFile['tmp_name']);

        // Destination infos
        $dest['x'] = $destX;
        $dest['y'] = $destY;
        $dest['width'] = $destWidth != NULL ? $destWidth : $src['width'];
        $dest['height'] = $destHeight != NULL ? $destHeight : $src['height'];
        $dest['ressource'] = createDestImage($dest['width'], $dest['height']);

        $white = imagecolorallocate($dest['ressource'], 255, 255, 255);
        imagecopyresampled($dest['ressource'], $src['ressource'], 0, 0, $dest['x'], $dest['y'], $dest['width'], $dest['height'], $dest['width'], $dest['height']);
        imagecolortransparent($dest['ressource'], $white);
        $return = $this->returnDestImage($fileType, $dest['ressource'], $destFile);
        return	($return);
    }
    /**
     *
     * @param type $type
     * @param type $filename
     * @return type 
     */
    function createSrcImage($type, $filename)
    {
        switch ($type)
        {
            case 1:
                return imagecreatefromgif($filename);
                break;
            case 3:
                return imagecreatefrompng($filename);
                break;
            case 2:
            default:
                return imagecreatefromjpeg($filename);
                break;
        }
    }
    /**
     *
     * @param type $width
     * @param type $height
     * @return type 
     */
    function createDestImage($width, $height){
        $image = imagecreatetruecolor($width, $height);
        $white = imagecolorallocate($image, 255, 255, 255);
        imagefill($image, 0, 0, $white);
        return $image;
    }

    /**
     * return array of differents value of image width
     * @return int 
     */
    public function getArrayWidthHeight(){
        $out = array();
        $out = array('meduim'=>250,'small'=>134,'big'=>300);
        return $out;
    }
    public function deleteFile($filename,$directory = _SITE_UPLOAD_DIR_){
        $filedel = $directory.$filename;
        if(file_exists($filedel)){
            unlink($filedel);
        }
                
    }
#########################
#Fin gestion des images #
#########################
    
}

?>
